package cn.edu.cug.cs.gtl.utils.impl;

import cn.edu.cug.cs.gtl.protos.DataCloud;
import cn.edu.cug.cs.gtl.protos.StorageCluster;
import cn.edu.cug.cs.gtl.utils.DataDeviceManager;
import cn.edu.cug.cs.gtl.utils.DatabaseUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DataDeviceManagerImpl implements DataDeviceManager {
    DatabaseUtil databaseUtil = new DatabaseUtil();

    @Override
    public void insertDataCloud(DataCloud dataCloud) {

    }


    @Override
    public DataCloud getDataCloud() {
        return null;
    }

    @Override
    public void insertStorageCluster(StorageCluster storageCluster) {
        Connection con = databaseUtil.getConnection();

        String insertSQL = "insert into storage_cluster(name, alias, description, type, url, user_name, password, dc_id) VALUES (?,?,?,?,?,?,?,?)";

        try {
            PreparedStatement preparedStatement = con.prepareStatement(insertSQL);
            preparedStatement.setString(1, storageCluster.getName());
            preparedStatement.setString(2, storageCluster.getAlias());
            preparedStatement.setString(3, storageCluster.getDescription());
            preparedStatement.setString(4, storageCluster.getType());
            preparedStatement.setString(5, storageCluster.getUrl());
            preparedStatement.setString(6, storageCluster.getUserName());
            preparedStatement.setString(7, storageCluster.getPassword());
            preparedStatement.setInt(8, 1);
            //preparedStatement.setString(8, storageCluster.getDcId());

            int rowsAffected = preparedStatement.executeUpdate();

            //检查是否成功插入一条记录
            if (rowsAffected > 0) {
                System.out.println(storageCluster.getName() + "插入成功");
            } else {
                System.err.println("插入失败");
            }

            preparedStatement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            databaseUtil.close(con);
        }
    }

    @Override
    public void updateStorageCluster(StorageCluster storageCluster, int id) {
        Connection con = databaseUtil.getConnection();

        String updateSQL = "update storage_cluster set name=?,alias=?,description=?,type=?,url=?,user_name=?,password=?,dc_id=? where id=?";

        try {
            PreparedStatement preparedStatement = con.prepareStatement(updateSQL);
            preparedStatement.setString(1, storageCluster.getName());
            preparedStatement.setString(2, storageCluster.getAlias());
            preparedStatement.setString(3, storageCluster.getDescription());
            preparedStatement.setString(4, storageCluster.getType());
            preparedStatement.setString(5, storageCluster.getUrl());
            preparedStatement.setString(6, storageCluster.getUserName());
            preparedStatement.setString(7, storageCluster.getPassword());
            preparedStatement.setInt(8, 1);
            //preparedStatement.setString(8, storageCluster.getDcId());

            preparedStatement.setInt(9, id);

            int rowsAffected = preparedStatement.executeUpdate();

            //检查是否成功插入一条记录
            if (rowsAffected > 0) {
                System.out.println(storageCluster.getName() + "更新成功");
            } else {
                System.err.println("更新失败");
            }

            preparedStatement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            databaseUtil.close(con);
        }
    }

    @Override
    public List<StorageCluster> getList() {
        Connection con = databaseUtil.getConnection();

        List<StorageCluster> list = new ArrayList<>();
        String selectSQL = "select * from storage_cluster";
        try {
            PreparedStatement statement = con.prepareStatement(selectSQL);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                String name = resultSet.getString(2);
                String alias = resultSet.getString(3);
                String description = resultSet.getString(4);
                String type = resultSet.getString(5);
                String url = resultSet.getString(6);
                String userName = resultSet.getString(7);
                String password = resultSet.getString(8);
                int dc_id = resultSet.getInt(9);

                StorageCluster storageCluster = StorageCluster.newBuilder()
                        .setName(name)
                        .setAlias(alias)
                        .setDescription(description)
                        .setType(type)
                        .setUrl(url)
                        .setUserName(userName)
                        .setPassword(password)
                        //暂时还没有dc_id
                        .build();

                list.add(storageCluster);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            databaseUtil.close(con);
        }
        return list;
    }

    @Override
    public int getIdByName(String storageClusterName) {
        Connection con = databaseUtil.getConnection();

        String selectSQL = "select id from storage_cluster where name = ?";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(selectSQL);
            preparedStatement.setString(1, storageClusterName);
            ResultSet resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                return resultSet.getInt("id");
            } else {
                System.err.println(storageClusterName + "不存在");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            databaseUtil.close(con);
        }
        return -1;
    }

    @Override
    public void removeStorageCluster(String storageClusterName) {
        Connection con = databaseUtil.getConnection();

        String deleteSQL = "delete from storage_cluster where name=?";


        try {
            PreparedStatement preparedStatement = con.prepareStatement(deleteSQL);

            preparedStatement.setString(1, storageClusterName);

            int rowsAffected = preparedStatement.executeUpdate();

            // 检查是否成功删除了一行记录，成功删除返回dataItem
            if (rowsAffected > 0) {
                System.out.println(storageClusterName + "删除成功");
            } else {
                System.err.println(storageClusterName + "删除失败");
            }

            preparedStatement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            databaseUtil.close(con);
        }
    }

}
